package com.taekwon.remoteservice;
import java.sql.SQLException;
import java.util.ArrayList;
import ibatis.MySqlMapper;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.taekwon.model.Fxboard;
public class FxboardDao {
	private static SqlMapClient sqlMapClient;
	public FxboardDao() {
		sqlMapClient = MySqlMapper.getSqlMapClient();
	}
	public ArrayList selectPaging(Fxboard fxboard) throws SQLException{
		System.out.println("FxboardDao[selectPaging]start");
		System.out.println("FxboardDao[selectPaging]fxboard:"+fxboard);
		System.out.println("FxboardDao[selectPaging]end");
		ArrayList arraylist = new ArrayList();
		try{
			sqlMapClient.startTransaction(); 
			arraylist = (ArrayList)sqlMapClient.queryForList("FxboardDao.selectPaging",fxboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			System.out.println("FxboardDao[selectPaging]error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		return arraylist;
	}
	public Fxboard selectPagingCount(Fxboard fxboard) throws SQLException{
		System.out.println("FxboardDao[selectPagingCount]start");
		System.out.println("FxboardDao[selectPagingCount]fxboard:"+fxboard);
		System.out.println("FxboardDao[selectPagingCount]end");
		Fxboard fxboard_ = new Fxboard();
		try{
			sqlMapClient.startTransaction(); 
			fxboard_ = (Fxboard)sqlMapClient.queryForObject("FxboardDao.selectPagingCount",fxboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			System.out.println("FxboardDao[selectPagingCount]error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		return fxboard_;
	}
	public int deleteFx(Fxboard fxboard) throws SQLException{
		System.out.println("FxboardDao[deleteFx]start");
		System.out.println("FxboardDao[deleteFx]fxboard:"+fxboard);
		System.out.println("FxboardDao[deleteFx]end");
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 
			int_ = (int)sqlMapClient.delete("FxboardDao.deleteFx",fxboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			System.out.println("FxboardDao[deleteFx]error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		return int_;
	}
	public int updateFx(Fxboard fxboard) throws SQLException{
		System.out.println("FxboardDao[updateFx]start");
		System.out.println("FxboardDao[updateFx]fxboard:"+fxboard);
		System.out.println("FxboardDao[updateFx]end");
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 
			int_ = (int)sqlMapClient.update("FxboardDao.updateFx",fxboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			System.out.println("FxboardDao[updateFx]error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		return int_;
	}
	public int insertFx(Fxboard fxboard) throws SQLException{
		System.out.println("FxboardDao[insertFx]start");
		System.out.println("FxboardDao[insertFx]fxboard:"+fxboard);
		System.out.println("FxboardDao[insertFx]end");
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 
			Fxboard fxboard_ = (Fxboard) sqlMapClient.queryForObject("FxboardDao.findMax",new Fxboard());
			fxboard.setThread(fxboard_.getCount());
			int_ = (int)sqlMapClient.update("FxboardDao.insertFx",fxboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			System.out.println("FxboardDao[insertFx]error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		return int_;
	}
	public int insertReplyFx(Fxboard fxboard) throws SQLException{
		System.out.println("FxboardDao[insertFx]start");
		System.out.println("FxboardDao[insertFx]fxboard:"+fxboard);
		System.out.println("FxboardDao[insertFx]end");
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 
			int param1 = fxboard.getThread();
			int param2 = ((int)((param1-1)/100))*100;
			fxboard.setThread(fxboard.getThread()-1);
			fxboard.setDepth(fxboard.getDepth()+1);
			Fxboard fxboardHierarchy = new Fxboard();
			
			System.out.println("param1:"+param1);
			System.out.println("param2:"+param2);
			
			fxboardHierarchy.setParam1(param1+"");
			fxboardHierarchy.setParam2(param2+"");
			int intUpdate = (int)sqlMapClient.update("FxboardDao.updateHierarchy",fxboardHierarchy);
			System.out.println("intUpdate:"+intUpdate);
			int_ = (int)sqlMapClient.update("FxboardDao.insertFx",fxboard); 
			System.out.println("int_:"+int_);
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			System.out.println("FxboardDao[insertFx]error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		return int_;
	}	
	public Fxboard findMax(Fxboard fxboard) throws SQLException{
		System.out.println("FxboardDao[findMax]start");
		System.out.println("FxboardDao[findMax]fxboard:"+fxboard);
		System.out.println("FxboardDao[findMax]end");
		Fxboard fxboard_ = new Fxboard();
		try{
			sqlMapClient.startTransaction(); 
			fxboard_ = (Fxboard)sqlMapClient.queryForObject("FxboardDao.findMax",fxboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			System.out.println("FxboardDao[findMax]error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		return fxboard_;
	}
	public int updateHierarchy(Fxboard fxboard) throws SQLException{
		System.out.println("FxboardDao[updateHierarchy]start");
		System.out.println("FxboardDao[updateHierarchy]fxboard:"+fxboard);
		System.out.println("FxboardDao[updateHierarchy]end");
		int int_ = 0;
		try{
			sqlMapClient.startTransaction(); 
			int_ = (int)sqlMapClient.update("FxboardDao.updateHierarchy",fxboard); 
			sqlMapClient.commitTransaction(); 
		}catch(Exception e){
			System.out.println("FxboardDao[updateHierarchy]error:"+e);
		}finally{
			sqlMapClient.endTransaction();
		}
		return int_;
	}
	public static void main(String[] args) throws SQLException {
		FxboardDao fxboarddao = new FxboardDao();
//		Fxboard fxboard = new Fxboard();
//		fxboard.setTitle("babo");
//		fxboard.setThread(2799);
//		fxboard.setDepth(1);
//		int count = fxboarddao.insertReplyFx(fxboard);
//		System.out.println("count:"+count);			
		
//		Fxboard fxboard = new Fxboard();
//		fxboard.setTitle("babo");
//		int count = fxboarddao.insertFx(fxboard);
//		count = fxboarddao.insertFx(fxboard);
//		System.out.println("count:"+count);		
//------------------------------------------------------------------		
//		FxboardDao fxboarddao = new FxboardDao();
//		Fxboard fxboard = new Fxboard();
//		int count = fxboarddao.findMax(fxboard).getCount();
//		System.out.println("count:"+count);				
//------------------------------------------------------------------
//		Fxboard fxboard = new Fxboard();
//		fxboard.setSeq(4);
//		fxboard.setTitle("babo");
//		int count = fxboarddao.updateFx(fxboard);
//		System.out.println("count:"+count);		
//------------------------------------------------------------------		
//		Fxboard fxboard = new Fxboard();
//		fxboard.setSeq(612);
//		int count = fxboarddao.deleteFx(fxboard);
//		System.out.println("count:"+count);
//------------------------------------------------------------------		
//		Fxboard fxboard = new Fxboard();
//		fxboard.setKeyword("안녕하세요");
//		int count = fxboarddao.selectPagingCount(fxboard).getCount();
//		System.out.println("count:"+count);
//------------------------------------------------------------------		
//		ArrayList<Fxboard> list = fxboarddao.selectPaging(fxboard);
//		for(int i=0;i<list.size();i++){
//			System.out.println(list.get(i).getTitle());
//		}
	}
}
